package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.dto.BrandCategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.PictureAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import cn.tedu.csmall.product.service.IPictureService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class PictureImpl implements IPictureService {

    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public void addNew(PictureAddNewDTO pictureAddNewDTO){
        log.debug("开始处里添加图片业务");
        Long albumId= pictureAddNewDTO.getAlbumId();
        AlbumStandardVO standardById = albumMapper.getStandardById(albumId);
        if (standardById==null){
            String message ="添加图片失败，没有这个相册";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }


        /**添加图片*/

        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewDTO,picture);
        int rows = pictureMapper.insert(picture);
        if (rows != 1 ){
            String message = "添加图片失败,服务器繁忙";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id){
        log.debug("开始处理删除图片的请求");
        /**判断是否存在*/
        int rows = pictureMapper.countById(id);
        if (rows == 0 ){
            String message = "删除图片失败，图片不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        rows = pictureMapper.deleteById(id);
        if (rows != 1 ){
            String message = "删除图片失败,服务器繁忙";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
